package com.topnet.leaderhelp.utils.xfyun.msc;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.URLSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.iflytek.cloud.msc.util.log.DebugLog;
import com.iflytek.cloud.resource.Resource;
import com.iflytek.cloud.ui.RecognizerDialogListener;
import com.iflytek.cloud.ui.b.e;
import com.topnet.leaderhelp.utils.xfyun.msc.ui.DLinearLayout;


/**
 * @Description: java类作用描述
 * @Author: 下雨了
 * @CreateDate: 2022-03-02 11:15
 * @UpdateUser: 更新者
 * @UpdateDate: 2022-03-02 11:15
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public final class ADialog extends DLinearLayout implements OnClickListener {
    private LinearLayout linearLayout;
    private e view = null;
    // 旋转动画
    private RotateAnimation rotateAnimation;
    // 语言识别
    private SpeechRecognizer speechRecognizer;
    private long i = 0L;

    private volatile int k;

    private RecognizerDialogListener recognizerDialogListener;

    public void setResultListener(RecognizerDialogListener resultListener) {
        this.recognizerDialogListener = resultListener;
        this.setOnClickListener(this);
    }

    // 识别监听
    private RecognizerListener recognizerListener = new RecognizerListener() {
        public void onBeginOfSpeech() {
        }

        public void onVolumeChanged(int param1Int, byte[] param1ArrayOfbyte) {
            if (ADialog.this.k == 1 && view != null) {
                int i = (param1Int + 2) / 5;
                view.setVolume(i);
                view.invalidate();
            }

        }

        // 识别结束
        public void onEndOfSpeech() {
            endOfSpeech();
        }

        public void onResult(RecognizerResult var1, boolean var2) {
            if (var2) {
                c();
            }

            if (ADialog.this.recognizerDialogListener != null) {
                ADialog.this.recognizerDialogListener.onResult(var1, var2);
            }

        }

        public void onError(SpeechError var1) {
//            if (var1 != null && MscDialog.h(MscDialog.this)) {
            if (var1 != null) {
                ADialog.this.setParameter(var1);
            } else {
                ADialog.this.c();
            }

            if (ADialog.this.recognizerDialogListener != null) {
                ADialog.this.recognizerDialogListener.onError(var1);
            }

        }

        public void onEvent(int var1, int var2, int var3, Bundle var4) {
        }
    };


    public ADialog(Context context, InitListener initListener) {
        super(context);
        this.speechRecognizer = SpeechRecognizer.createRecognizer(context.getApplicationContext(), initListener);
        this.show(context);
    }

    // 识别回调
    private void i() {
        DebugLog.LogD("startRecognizing");
        long var1 = this.i;
        long var3;
        long var10000 = var3 = SystemClock.elapsedRealtime();
        this.i = var3;
        if (var10000 - var1 >= 300L) {
            this.speechRecognizer.setParameter("msc.skin", "default");
            int var7;
            if ((var7 = this.speechRecognizer.startListening(this.recognizerListener)) != 0) {
                ADialog var5 = this;
                SpeechError var6;
                var6 = new SpeechError(var7);
                var5.setParameter(var6);
            } else {
                f();
            }
        }
    }

    private void j() {
        LinearLayout var1;
        if ((var1 = this.linearLayout) != null) {
            var1.destroyDrawingCache();
            this.linearLayout = null;
        }

        this.view = null;
        System.gc();
    }

    private void f() {
        if (this.view == null) {
            this.view = new e(this.getContext().getApplicationContext());
        }

        this.k = 1;
        showVisibility();
    }

    private void endOfSpeech() {
        try {
            this.linearLayout.findViewWithTag("waiting").findViewWithTag("control").startAnimation(this.rotateAnimation);
            this.k = 2;
            showVisibility();
        } catch (Exception var1) {
            DebugLog.LogE(var1);
        }

    }

    private void setParameter(SpeechError param1) {
        // $FF: Couldn't be decompiled
    }

    private void showVisibility() {
        FrameLayout frameLayout = linearLayout.findViewWithTag("waiting");
        TextView var2 = linearLayout.findViewWithTag("title");
        LinearLayout var3 = linearLayout.findViewWithTag("error");
        TextView var4 = frameLayout.findViewWithTag("tips");
        if (this.k == 1) {
            var3.setVisibility(View.GONE);
            var2.setVisibility(View.VISIBLE);
            frameLayout.setVisibility(View.GONE);
            var2.setText(Resource.getTitle(2));
            this.view.setVolume(0);
            this.view.invalidate();
            this.view.setVisibility(View.VISIBLE);
        } else if (this.k == 2) {
            var2.setVisibility(View.GONE);
            this.view.setVisibility(View.GONE);
            frameLayout.setVisibility(View.VISIBLE);
            var4.setVisibility(View.VISIBLE);
            var4.setText(Resource.getTitle(3));
        } else if (this.k == 3) {
            var2.setVisibility(View.GONE);
            this.view.setVisibility(View.GONE);
            frameLayout.setVisibility(View.GONE);
            var3.setVisibility(View.VISIBLE);
        }
    }

    // 显示UI
    public void show(Context context) {
        LayoutParams layoutParams;

        try {
            View var8 = com.iflytek.cloud.ui.b.a.a(context, "recognize", this);
            var8.setBackground(com.iflytek.cloud.ui.b.a.b(context.getApplicationContext(), "voice_bg.9"));
            TextView textView = var8.findViewWithTag("textlink");
            textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
            textView.setText("语音识别能力由讯飞输入法提供");
            textView.setLinksClickable(true);
            textView.setOnClickListener(new OnClickListener() {
                public void onClick(View var1x) {
                    Uri uri;
                    Intent intent;
                    try {
                        uri = Uri.parse("http://www.xunfei.cn/?appid=" + com.iflytek.cloud.msc.util.e.b(com.iflytek.cloud.b.c.a()));
                        intent = new Intent("android.intent.action.VIEW", uri);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                        k = 1;
                        showVisibility();
                        speechRecognizer.stopListening();
                        f();
                        context.startActivity(intent);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    DebugLog.LogW("failed");
                }
            });
            linearLayout = var8.findViewWithTag("container");
            com.iflytek.cloud.msc.util.g.a(linearLayout);
            view = new e(context);
            layoutParams = new LayoutParams(-1, 0, 1.0F);
            layoutParams.bottomMargin = 20;

            linearLayout.addView(view, 1, layoutParams);
            linearLayout.findViewWithTag("waiting").findViewWithTag("control").setBackgroundDrawable(com.iflytek.cloud.ui.b.a.b(this.getContext(), "waiting"));
            rotateAnimation = new RotateAnimation(0.0F, 360.0F, 1, 0.5F, 1, 0.5F);
            rotateAnimation.setRepeatCount(-1);
            rotateAnimation.setInterpolator(new LinearInterpolator());
            rotateAnimation.setDuration(700L);
            linearLayout.setVisibility(View.VISIBLE);
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    public void setParameter(TextView var1, SpeechError var2) {
        boolean var3 = true;
        String var4;
        if ((var4 = this.speechRecognizer.getParameter("view_tips_plain")) != null && (var4.equalsIgnoreCase("false") || var4.equalsIgnoreCase("0"))) {
            var3 = false;
        }

        var1.setText(Html.fromHtml(var2.getHtmlDescription(var3)));
        var1.setMovementMethod(LinkMovementMethod.getInstance());
        var1.bringToFront();
        CharSequence var15;
        if ((var15 = var1.getText()) instanceof Spannable) {
            int var5 = var15.length();
            Spannable var6;
            URLSpan[] var7;
            URLSpan[] var10000 = var7 = (URLSpan[]) (var6 = (Spannable) var1.getText()).getSpans(0, var5, URLSpan.class);
            SpannableStringBuilder var8;
            SpannableStringBuilder var10001 = var8 = new SpannableStringBuilder();
            var10001 = new SpannableStringBuilder(var15);
            var10001.clearSpans();
            int var16 = var10000.length;

            for (int var9 = 0; var9 < var16; ++var9) {
                URLSpan var10 = var7[var9];
                MyClickableSpan myClickableSpan;
                myClickableSpan = new MyClickableSpan(var10.getURL());
                int var12 = var6.getSpanStart(var10);
                int var17 = var6.getSpanEnd(var10);
                var8.setSpan(myClickableSpan, var12, var17, 34);
            }

            int var13 = var2.getHtmlDescription(false).length();
            int var14 = var2.getHtmlDescription(true).length() - 4;
            var8.setSpan(new ForegroundColorSpan(com.iflytek.cloud.ui.b.a.a()[0]), 0, var13, 34);
            var8.setSpan(new AbsoluteSizeSpan(com.iflytek.cloud.ui.b.a.b()[0], true), 0, var13, 33);
            if (var3) {
                ForegroundColorSpan var10002 = new ForegroundColorSpan(com.iflytek.cloud.ui.b.a.a()[1]);
                ++var13;
                var8.setSpan(var10002, var13, var14 + 1, 34);
                var8.setSpan(new AbsoluteSizeSpan(com.iflytek.cloud.ui.b.a.b()[1], true), var13, var5, 34);
            }

            var1.setText(var8);
        }

    }

    public void onClick(View var1) {
        int var2;
        if ((var2 = this.k) != 1) {
            if (var2 == 3) {
                if (var1.getTag() != null && ((SpeechError) var1.getTag()).getErrorCode() == 20001) {
                    this.d();
                } else {
                    this.i();
                }
            }
        } else {
            this.speechRecognizer.stopListening();
            this.endOfSpeech();
        }

    }

    public void e() {
        super.e();
        this.i();
    }

    public void a() {
        if (this.speechRecognizer.isListening()) {
            this.speechRecognizer.cancel();
        }

        super.a();
    }

    protected boolean b() {
        if (super.b()) {
            j();
            // 摧毁
            return speechRecognizer.destroy();
        } else {
            return false;
        }
    }

    public class MyClickableSpan extends ClickableSpan {
        private String a;

        public MyClickableSpan(String var2) {
            this.a = var2;
        }

        public void onClick(View param1) {
            // $FF: Couldn't be decompiled
        }

        public void updateDrawState(TextPaint var1) {
            super.updateDrawState(var1);
        }
    }

    // ========================= 对外暴漏 ==========================================================================================

    public void setTitle(CharSequence var1) {
        ((TextView) this.linearLayout.findViewWithTag("title")).setText(var1);
    }

    public void setParameter(String var1, String var2) {
        this.speechRecognizer.setParameter(var1, var2);
    }

}

